﻿using NDK.Module.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NDK.Frm
{
    public partial class Dlg_ManualDownOperate : Form
    {
        public Dlg_ManualDownOperate()
        {
            InitializeComponent();
            cbListInit();
        }
        private List<CheckBox> cbCauseList = new List<CheckBox>();
        private List<CheckBox> cbActionList = new List<CheckBox>();

        public AlarmInfo alarmInfo = new AlarmInfo();
        private string CauseStr = "";
        private string ActionStr = "";



        private static Dlg_ManualDownOperate _instance;
        private static readonly object singletonLock = new object();
        public static Dlg_ManualDownOperate Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Dlg_ManualDownOperate();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void cbListInit()
        {
            cbCauseList.Add(cb_Cause_None);
            cbCauseList.Add(cb_Cause_UnitFailures);
            cbCauseList.Add(cb_Cause_Tossing);
            cbCauseList.Add(cb_Cause_MeterialIssues);
            cbCauseList.Add(cb_Cause_Others);
            cbCauseList.Add(cb_Cause_None);

            cbActionList.Add(cb_Action_PreMaintenance);
            cbActionList.Add(cb_Action_MachineTuning);
            cbActionList.Add(cb_Action_VisionTuning);
            cbActionList.Add(cb_Action_ComputerTroubleShooting);
            cbActionList.Add(cb_Action_Others);

            foreach (CheckBox cb in cbCauseList)
            {
                cb.CheckedChanged += new System.EventHandler(this.cb_Cause_CheckedChanged);
            }

            foreach (CheckBox cb in cbActionList)
            {
                cb.CheckedChanged += new System.EventHandler(this.cb_Action_CheckedChanged);
            }
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            alarmInfo.EndTime= DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            int timeCount = (alarmInfo.EndTime - alarmInfo.StartTime).Seconds;
            alarmInfo.TimeCount = timeCount;
            alarmInfo.Type = $"{CauseStr}-{ActionStr}";
            alarmInfo.Description = $"{CauseStr}-{ActionStr}-{richtxt_AdditionalInfo.Text}";

            NDK.Module.DAL.AlarmInfoServices AlarmInfoServices = new NDK.Module.DAL.AlarmInfoServices();
            AlarmInfoServices.Add(alarmInfo);
            this.Close();
        }

        private void cb_Cause_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;

            if (cb.Checked)
            {
                foreach (CheckBox item in cbCauseList)
                {
                    if (item != cb)
                        item.Checked = false;
                }
            }
            ConfirmEnable();
        }

        private void cb_Action_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;

            if (cb.Checked)
            {
                foreach (CheckBox item in cbActionList)
                {
                    if (item != cb)
                        item.Checked = false;
                }
            }
            ConfirmEnable();
        }

        private void ConfirmEnable()
        {
            bool isCauseChecked = false;
            bool isActionChecked = false;

            foreach (CheckBox item in cbCauseList)
            {
                if (item.Checked)
                {
                    isCauseChecked = true;
                    CauseStr = item.Text;
                    break;
                }
            }
            foreach (CheckBox item in cbActionList)
            {
                if (item.Checked)
                {
                    isActionChecked = true;
                    ActionStr = item.Text;
                    break;
                }
            }
            if (isCauseChecked && isActionChecked)
            {
                btn_Confirm.Enabled = true;
            }
            else
            {
                btn_Confirm.Enabled = false;
            }
        }
    }
}
